package com.xiaobias.entity;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 阻塞队列，作为日志系统输出的日志的一个临时载体
 */
public class LoggerQueue {
    private static final LoggerQueue alarmMessageQueue = new LoggerQueue();

    //队列大小
    public static final int QUEUE_MAX_SIZE = 10000;

    //阻塞队列
    private BlockingQueue<LoggerMessage> blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE);

    private LoggerQueue() {
    }

    public synchronized static LoggerQueue getInstance() {
        return alarmMessageQueue;
    }

    /**
     * 消息入队
     */
    public void push(LoggerMessage log) {
        this.blockingQueue.add(log);
    }

    /**
     * 消息出队
     */
    public LoggerMessage poll() {
        LoggerMessage result = null;
        try {
            result = this.blockingQueue.take();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
    }
}